package com.source.hotelbath.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ffcs.network.connect.CustomerError;
import com.ffcs.network.connect.ResponseListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.source.hotelbath.R;
import com.source.hotelbath.base.BizActivity;

import com.source.hotelbath.model.GoodsListBean;
import com.source.hotelbath.net.CustomerRequestConstants;
import com.source.hotelbath.net.LoadingDialog;
import com.source.hotelbath.net.request.HotelGoodListRequest;
import com.source.hotelbath.net.response.HotelGoodListResponse;
import com.source.hotelbath.net.response.HotelListResponse;
import com.source.hotelbath.utils.DialogCallback;
import com.source.hotelbath.weight.listview.ptr.PtrMaterialHeader;
import com.source.hotelbath.weight.listview.ptr.ptrloadmore.PtrLoadMoreBase;
import com.source.hotelbath.weight.listview.ptr.ptrloadmore.PtrLoadMoreListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class LinenListActivity extends BizActivity implements PtrLoadMoreBase.LoadingListener{

    public static final String TAG = LinenListActivity.class.getSimpleName();
    private LinearLayout ll_header_right;
    private PtrLoadMoreListView ptlv_content;
    private View mHeaderView;
    private LinearLayout ll_header_left;
    private TextView tv_header_right;
    private List<GoodsListBean> commonGoods=new ArrayList<>();

    private LoadingDialog dialog;
    private  boolean isRefresh =true;
    private LinenListAdapter adapter;

    public Map<GoodsListBean, Integer> map = new HashMap<GoodsListBean, Integer>(); //记录被选择的列表
    private LocalBroadcastManager localBroadcastManager;
    private HotelGoodListResponse listResponse;
    private int hotelId;

    @Override
    protected void onBaseCreate(Bundle var1) {
        setContentView(R.layout.activity_linen_list);
    }

    @Override
    protected void bindView(Bundle var1) {
        commonlist=  getIntent().getParcelableArrayListExtra("commonlist");
        hotelId=getIntent().getIntExtra("hotelId",0);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(CustomerRequestConstants.ALLGOODS);
        localBroadcastManager.registerReceiver(mReceiver,myIntentFilter);
        ll_header_left = (LinearLayout) findViewById(R.id.ll_header_left);

        ll_header_right = (LinearLayout) findViewById(R.id.ll_header_right);
        tv_header_right = (TextView) findViewById(R.id.tv_header_right);
        tv_header_right.setText("添加");
        ll_header_right.setOnClickListener(this);
        ll_header_left.setOnClickListener(this);
        TextView tv_header_title= (TextView) findViewById(R.id.tv_header_title);
        tv_header_title.setText("常用商品");
        ptlv_content = (PtrLoadMoreListView) findViewById(R.id.ptlv_content);

        initHeaderView();
        ptlv_content.setLastUpdateKey(TAG);
        ptlv_content.getRefreshView().setVerticalScrollBarEnabled(false);
        ptlv_content.registListener(this);
        ptlv_content.disableWhenHorizontalMove(true);

        ptlv_content.setHeader(new PtrMaterialHeader(this));
        ptlv_content.getPtrFrame().setPinContent(true);
        if (null != mHeaderView) {
            ptlv_content.getRefreshView().addHeaderView(mHeaderView);
        }
        adapter = new LinenListAdapter(this,commonGoods);
        ptlv_content.getRefreshView().setAdapter(adapter);

        ptlv_content.setPageSize(15);
        ptlv_content.loadSuccess();
        dialog = new LoadingDialog(this);

    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view==ll_header_left){
            finish();
        }else if(view==ll_header_right){
            Intent intent = new Intent(this,TitleLinenActivity.class);

            intent.putParcelableArrayListExtra("commonGoods", (ArrayList<? extends Parcelable>) commonGoods);
            startActivity(intent);
        }
    }

    private void initHeaderView() {
        mHeaderView = LayoutInflater.from(this).inflate(R.layout.linen_list_header, null);
    }
    private List<GoodsListBean> commonlist=new ArrayList<>();
    @Override
    protected void afterCreate() {
        super.afterCreate();

       if (commonlist!=null&&commonlist.size()>0){
           commonGoods.clear();
           commonGoods.addAll(commonlist);
           adapter.notifyDataSetChanged();
           ptlv_content.loadSuccess();
           ptlv_content.setHasNext(false);
           isRefresh=false;
       }else {
           new Handler().postDelayed(new Runnable() {
               @Override
               public void run() {
                   ptlv_content.autoRefresh();
               }
           },300);
       }

    }



    @Override
    public void refresh(int pageIndex, int pageSize) {
        if (isRefresh){

            OkGo.<HotelGoodListResponse>get(CustomerRequestConstants.newUrl+CustomerRequestConstants.GOODS_HOTEL_COMMOND+"?hotelId="+hotelId)//
                    .tag(this)//
                    .execute(new DialogCallback<HotelGoodListResponse>(LinenListActivity.this,false) {
                        @Override
                        public void onSuccess(Response<HotelGoodListResponse> response) {
                            HotelGoodListResponse responses= response.body();
                            if (responses!=null&&responses.getCommonGoods()!=null&&responses.getCommonGoods().size()>0){
                                commonGoods.clear();
                                commonGoods.addAll(responses.getCommonGoods());
                                adapter.notifyDataSetChanged();
                                ptlv_content.loadSuccess();
                                ptlv_content.setHasNext(false);
                                isRefresh=false;
                            }else {
                                ptlv_content.loadFailure();
                                ptlv_content.showEmptyView();
                            }

                        }

                        @Override
                        public void onError(Response<HotelGoodListResponse> response) {
                            super.onError(response);
                            ptlv_content.loadSuccess();
                            Toast.makeText(LinenListActivity.this,"请求网络失败",Toast.LENGTH_SHORT).show();
                        }

                    });
        }else {
            ptlv_content.loadSuccess();
        }

    }

    @Override
    public void loadMore(int pageIndex, int pageSize) {

    }


    private BroadcastReceiver mReceiver  = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (CustomerRequestConstants.ALLGOODS.equals(intent.getAction())){
                //添加集合
                List<GoodsListBean> commonGood =intent.getParcelableArrayListExtra("commonGoods");
                if (commonGood!=null){
                    commonGoods.addAll(commonGood);
                    adapter.notifyDataSetChanged();
                }

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(mReceiver);
    }
}
